/**
 * 积分相关API
 */
const { request } = require('./request');

/**
 * 获取充值套餐列表
 * @returns {Promise} - 返回Promise
 */
function getRechargePackages() {
  return request('/points/packages', {});
}

/**
 * 创建充值订单
 * @param {Number} packageId - 套餐ID
 * @returns {Promise} - 返回Promise
 */
function createRechargeOrder(packageId) {
  return request('/points/create-order', { packageId });
}

/**
 * 发起微信支付
 * @param {String} orderId - 订单ID
 * @returns {Promise} - 返回Promise
 */
function wxPay(orderId) {
  return new Promise((resolve, reject) => {
    request('/points/wx-pay', { orderId })
      .then(res => {
        // 调用微信支付
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.paySign,
          success: function(result) {
            resolve(result);
          },
          fail: function(err) {
            reject(err);
          }
        });
      })
      .catch(err => {
        reject(err);
      });
  });
}

/**
 * 查询支付结果
 * @param {String} orderId - 订单ID
 * @returns {Promise} - 返回Promise
 */
function queryPayResult(orderId) {
  return request('/points/query-pay', { orderId });
}

module.exports = {
  getRechargePackages,
  createRechargeOrder,
  wxPay,
  queryPayResult
}; 